package com.yst.sqlbuilder;

import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.util.Objects;

public interface IParentedSelect<T> extends ISQLExpression,IAliasExpression {
    IParentedSelect<T> getParentSelect();
    default IParentedSelect<T> getRoot(){
        if (Objects.isNull(getParentSelect())) return this;
        IParentedSelect<T> result=getParentSelect();
        while (Objects.nonNull(result)&&Objects.nonNull(result.getParentSelect())){
            result=result.getParentSelect();
        }
        return result;
    }

    default String registerParam(Object value){
        if (getRoot()==this){
            throw new NotImplementedException();
        }
        return getRoot().registerParam(value);
    }

    default int getLevel(){
        if (Objects.nonNull(getParentSelect())){
            return getParentSelect().getLevel()+1;
        }
        return 0;
    }
}
